﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System.Data.SqlClient;
using System.Collections;
using System.Threading;

namespace AtualizadorScripts
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }

        Initial initial = new Initial();
        //Myconnection db = new Myconnection();
        public DataTable parametros = new DataTable();

        //string ConnectionString = db.ConnStr;

        string ARQUIVOS_COMUNS = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
        string ARQUIVOS_COMUNSX86 = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86);
        string LinxPOSShared = @"\Linx Sistemas\Shared\";
        string strDefauldir = @"L:\LinxPOS";
        string servicePackComponentes = @"\ServicePack\Components\";
        string p_versao_service_pack_loja = string.Empty;
        string p_valor_atual = string.Empty;
        string packBinInfo = string.Empty;
        string parametro = string.Empty;
        public DataRow[] foundRows;


        public void AtualizarScripts()
        {
         
            try
            {
                    if (packBinInfo != p_versao_service_pack_loja)
                    {
                        //MessageBox.Show("São diferentes\n" + packBinInfo + p_versao_service_pack_loja);

                        //string[] arFileTypes = { "*.dll", "*.ocx" };
                        string[] files = System.IO.Directory.GetFiles(strDefauldir + @"\ServicePack\Scripts\", "*.sql");
                        int Maximum = 0;
                        for (int i = 1; i <= files.Length; i++)
                        {
                            Maximum = i;
                        }
                        progressBar1.Maximum = Maximum ;
                        progressBar1.Value = progressBar1.Minimum;
                        foreach (string i in files)
                        {
                            label1.Text = i;
                            label1.Refresh();
                            
                            FileInfo file = new FileInfo(i.ToString());
                            
                            string script = file.OpenText().ReadToEnd();
                           
                            using (SqlConnection connection = Conexao.getConnection())
                            {
                   
                                using (SqlCommand cmd = new SqlCommand(script, connection))
                                {
                               
                                    connection.Open();
                                    IAsyncResult result = cmd.BeginExecuteReader();
                                    while (!result.IsCompleted)
                                    {
                                    
                                        Thread.Sleep(50);
                                        //progressBar1.Value++;
                                        //Make thread wait 5 seconds (or whatever you desire) so you aren't maxing out CPU by looping constantly
                                    }
                                    //cmd.EndExecuteReader();
                                    //cmd.EndExecuteReader(result);
                                    SqlDataReader dr = cmd.EndExecuteReader(result);
                                }
                            }
                            progressBar1.Value++;
                        
                        }
                        initial.SaveParameters("VERSAO_SERVICE_PACK_LOJA", packBinInfo);
                        MessageBox.Show("Terminou a atualizacao !");
                }

            }
            catch (SqlException sqlExc)
            {
                MessageBox.Show(sqlExc.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
           
        
               


        private void button1_Click(object sender, EventArgs e)
        {
            p_versao_service_pack_loja = GetParameters("PARAMETRO ='VERSAO_SERVICE_PACK_LOJA'");
            initial.SaveParameters("VERSAO_SERVICE_PACK_LOJA", "7.1.1");
                   AtualizarScripts();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Thread backgroundThread = new Thread(
            //new ThreadStart(() =>
            //{
            //    for (int n = 0; n < 100; n++)
            //    {
            //        Thread.Sleep(50);
            //        progressBar1.BeginInvoke(
            //            new Action(() =>
            //            {
            //                progressBar1.Value = n;
            //            }
            //        ));
            //    }

            //    MessageBox.Show("Thread completed!");
            //    progressBar1.BeginInvoke(
            //        new Action(() =>
            //        {
            //            progressBar1.Value = 0;
            //        }
            //    ));
            //}
            //));
            //        backgroundThread.Start();
        }

        public string GetParameters(string parametro) 
        {
            foundRows = parametros.Select(parametro);
            foreach (DataRow row in foundRows)
            {
                parametro = row[1].ToString();
            }

            return parametro;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            parametros = initial.LoadParameters();

            packBinInfo = System.IO.File.ReadAllText(strDefauldir + @"\ServicePack\PackInfo.bin");

            p_versao_service_pack_loja = GetParameters("PARAMETRO ='VERSAO_SERVICE_PACK_LOJA'");
            
        }
    }
}
